package com.kmzx.one.project.net;

import com.kmzx.one.common.action.ActionMap;
import com.kmzx.one.common.action.ActionsLog;
import com.kmzx.one.common.action.ResetLog;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
@ApiModel
public class BaseResult extends ActionMap implements Serializable {

    @Override
    public void init() {
        // 记录数据保存日志
        putMethod("default", "defaultMethod");
    }

    private static final long serialVersionUID = 5029637186189306114L;

    //@ApiModelProperty(name = "响应码", value = "响应码",  example = "201")
    protected Integer code;

   // @ApiModelProperty(name = "响应信息", value = "响应信息", example = "数据为空")
    protected String msg;

   // @ApiModelProperty(name = "响应错误码", value = "响应错误码", example = "201")
    protected Integer errCode;

   // @ApiModelProperty(name = "响应错误信息", value = "响应错误信息",example = "数据为空")
    protected String errMsg;

   // @ApiModelProperty(name = "响应数据", value = "响应数据",  example = "{userId:uuid_123456}")
    protected Object data;

    public Integer getCode() {
        return code;
    }

    public BaseResult setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public BaseResult setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Integer getErrCode() {
        return errCode;
    }

    public BaseResult setErrCode(Integer errCode) {
        this.errCode = errCode;
        return this;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public BaseResult setErrMsg(String errMsg) {
        this.errMsg = errMsg;
        return this;
    }

    public Object getData() {
        return data;
    }

    public BaseResult setData(Object data) {
        this.data = data;
        return this;
    }


    public BaseResult initSuccCodeAndMsg(Integer code, String msg) {
        BaseResult baseResult = Builder.baseResult;
        baseResult.errCode = code;
        baseResult.errMsg = msg;
        baseResult.code = code;
        baseResult.msg = msg;
        return baseResult;
    }


    public BaseResult initErrCodeAndMsg(Integer code, String msg) {
        BaseResult baseResult = Builder.baseResult;
        baseResult.errCode = code;
        baseResult.errMsg = msg;
        baseResult.code = code;
        baseResult.msg = msg;
        return baseResult;
    }

    public BaseResult initSuccData(Object data){
        BaseResult baseResult = Builder.SUCC();
        baseResult.setData(data);
        return baseResult;
    }

    public BaseResult initSuccData2(Object data) {
        BaseResult baseResult = Builder.SUCC();
        if (data != null) {
            baseResult.data = data;
            if (data instanceof List) {
                if (CollectionUtils.isEmpty((List) data)) {
                    baseResult.setCode(201);
                    baseResult.setErrCode(201);
                    baseResult.setMsg("数据为空");
                    baseResult.setErrMsg("数据为空");
                    baseResult.setData(null);
                }
            } else if (data instanceof Map) {
                if (CollectionUtils.isEmpty((Map) data)) {
                    baseResult.setCode(201);
                    baseResult.setData(null);
                    baseResult.setErrCode(201);
                    baseResult.setMsg("数据为空");
                    baseResult.setErrMsg("数据为空");
                }
            }
        } else {
            baseResult.setData(null);
            baseResult.setCode(201);
            baseResult.setErrCode(201);
            baseResult.setMsg("数据为空");
            baseResult.setErrMsg("数据为空");
        }
        return baseResult;
    }

    public BaseResult initErrorCode(Integer code) {
        BaseResult baseResult = Builder.baseResult;
        baseResult.errCode = code;
        baseResult.code = code;
        return baseResult;
    }

    public BaseResult initErrorMsg(String msg) {
        BaseResult baseResult = Builder.FAIL();
//        baseResult.errCode = 500;
        baseResult.errMsg = msg;
        baseResult.msg = msg;
        return baseResult;
    }


    private BaseResult() {
    }

    public static class Builder {
        private static BaseResult baseResult = new BaseResult();

        private Builder() {
        }

        public static BaseResult SUCC() {
            baseResult.errCode = 200;
            baseResult.code = 200;
            baseResult.errMsg = "OK";
            baseResult.msg = "OK";
            baseResult.data = null;
            return baseResult;
        }

        public static BaseResult FAIL() {
            baseResult.errCode = 400;
            baseResult.code = 400;
            baseResult.errMsg = "服务器正忙";
            baseResult.msg = "服务器正忙";
            baseResult.data = null;
            return baseResult;
        }

        public static BaseResult FAILServer() {
            baseResult.errCode = 500;
            baseResult.code = 500;
            baseResult.errMsg = "服务器正忙";
            baseResult.msg = "服务器正忙";
            baseResult.data = null;
            return baseResult;
        }
    }


    /**
     * 重新包装保存的数据行为方法
     *
     * @param resetLog ResetLog对象数据
     */
    public static void defaultMethod(ResetLog resetLog) {
        ActionsLog actionLog = resetLog.getActionsLog();
        Object entity = resetLog.getEntityParam();
        Assert.notNull(entity, "日志记录失败：未发现@EntityParam注解参数，将不做数据日志记录");


        // 获取实体对象数据ID
//        Object[] idInfo = EntityBeanUtil.getId(entity);
//        Assert.notNull(idInfo, "无法获取该实体对象的主键ID字段！");
//        actionLog.setRecordId(Long.valueOf(String.valueOf(idInfo[1])));
//
        // 日志消息
//        String idParam = ServletUtils.getParameter(String.valueOf(idInfo[0]));
//        String message = "数据成功";
//        if (!actionLog.getMessage().isEmpty()) {
//            message = resetLog.fillRule(entity, actionLog.getMessage());
//        }
//
//        if (idParam == null) {
//            actionLog.setMessage("添加" + message);
//        } else {
//            actionLog.setMessage("更新" + message);
//        }
    }

}
